class OrderOption < ActiveRecord::Base  
  has_many                  :order_extras
  has_many                  :orders, :through => :order_extras
  validates_presence_of     :name
  validates_presence_of     :cost
  validates_numericality_of :cost
  
  after_destroy             :destroy_dependents
  
  def included_in?(order)
    !order.order_extras.detect{|order_extra| order_extra.order_option == self}.nil?
  end
  
  def has_quantity_in?(quantity, order)
    order.order_extras.detect{|order_extra| (order_extra.quantity.to_s == quantity.to_s) if (order_extra.order_option == self)}
  end
  
  def quantity_in?(order)
    order.order_extras.each {|order_extra| (return order_extra.quantity) if (order_extra.order_option == self)}
    return nil
  end
protected
  def destroy_dependents
    self.order_extras.each {|order_extra| order_extra.destroy} unless self.order_extras.empty?
  end
end
